package com.fenyin.samples.study.jdk.nio.channels.tcp.server.reactor;  

import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 *@Description:  Socket处理异步线程
 *@Author:fengzhichao  
 *@Since:2012-12-3  
 *@Version:1.1.0
 */
public class Acceptor implements Runnable{
	/**Socket通信通道*/
	private ServerSocketChannel serverSocketChannel = null;
	/**事件监听器*/
	private Selector selector = null;  
	
	/**日志处理实例*/
	private static final Log log = LogFactory.getLog(Acceptor.class);
	
	public Acceptor(ServerSocketChannel serverSocketChannel,Selector selector){
		this.serverSocketChannel = serverSocketChannel;
		this.selector=selector;
	}
	
	public void run() {
		try{
			SocketChannel socketChannel = serverSocketChannel.accept();
			if (socketChannel != null){
				log.info("接受来自客户端（"+socketChannel.socket().getInetAddress().getHostAddress() + "）的连接");  
				//该处业务逻辑可以使用线程池技术处理
				new Handler(selector, socketChannel);
//                new Thread(new Handler(selector, socketChannel)).start();
            }  

		}catch(Throwable th){
			log.error("监听异常", th);
		}
	}

}
